
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Diagnostics;

using Swaf;


namespace Swaf.Container
{
	/// <summary>
	/// Summary description for ModelReferenceNode.
	/// </summary>
	public class ModelReferenceNode : INameAddressSupport
	{
		protected INameAddressSupport m_root;
		protected string m_rootReference;
		protected object m_referenced;

		public ModelReferenceNode(INameAddressSupport root, string rootReference)
		{
			m_root = root;
			m_rootReference = rootReference;
		}

		public ModelReferenceNode(INameAddressSupport root)
		{
			m_root = root;
		}

		protected object Reference 
		{
			get
			{
				if(m_referenced == null)
					if(m_rootReference != null)
						m_referenced = m_root.get(m_rootReference);	
					else
						m_referenced = m_root;
				return m_referenced;
			}
		}

		#region INameAddressSupport Members

		/// <summary>
		/// </summary>
		/// <param name="nameAddress"></param>
		/// <returns></returns>
		public bool containsField(string nameAddress)
		{
			object myref = Reference;
			INameAddressSupport el = null;
			if(myref is INameAddressSupport)
				el = (INameAddressSupport)myref;

			if(nameAddress == null || nameAddress.Length == 0)
				return myref != null;
			if(el != null)
				return el.containsField(nameAddress);
			return false;
		}

		public bool isGetOnly
		{
			get
			{
				return false;
			}
		}

		public Swaf.Container.XmlSerializationSupport SerializationSupport
		{
			get
			{
				// TODO:  Add ModelReferenceNode.SerializationSupport getter implementation
				return new Swaf.Container.XmlSerializationSupport ();
			}
		}

		public void put(string nameAddress, object val, object extraInfo)
		{
			Debug.Assert(nameAddress != null && nameAddress.Length > 0);
			if(Reference is INameAddressSupport)
				((INameAddressSupport)Reference).put(nameAddress, val, extraInfo);
		}

		void Swaf.Container.INameAddressSupport.put(string nameAddress, object val)
		{
			Debug.Assert(nameAddress != null && nameAddress.Length > 0);
			if(Reference is INameAddressSupport)
				((INameAddressSupport)Reference).put(nameAddress, val);
		}

		public string getXml(string options)
		{
			if(Reference is INameAddressSupport)
				return ((INameAddressSupport)Reference).getXml(options);
			return "";
		}

		public object get(string nameAddress, object oneArg)
		{
			return this.get(nameAddress, new object[]{oneArg});
		}

		public object get(string nameAddress)
		{
			return this.get(nameAddress, new object[]{});
		}

		public object get(string nameAddress, params object[] infoArgs)
		{
			if(nameAddress == null || nameAddress.Length == 0)
				return Reference;
			else if(Reference is INameAddressSupport)
				return ((INameAddressSupport)Reference).get(nameAddress, infoArgs);

			return null;
		}

		public string Xml
		{
			get
			{
				if(Reference is INameAddressSupport)
					return ((INameAddressSupport)Reference).Xml;
				return "";
			}
		}

		#endregion
		public IEnumerator GetEnumerator() { return null; }
	}
}
